﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZSZ.DTO;
using ZSZ.IService;
using ZSZ.Model;

namespace ZSZ.Service
{
    public partial class IdNameService : BaseService<IdName>, IIdNameService
    {
        public IdNameDTO[] GetAll(string typeName)
        {
            return GetAll().Where(e => e.TypeName == typeName)
                .ToList().Select(e => ToDTO(e)).ToArray();
        }

        public IdNameDTO GetDTOById(long id)
        {
            var idNames = GetAll().Where(e => e.Id == id);
            int count = idNames.Count();
            if (count <= 0)
            {
                return null;
            }
            else if (count == 1)
            {
                return ToDTO(idNames.Single());
            }
            else
            {
                throw new ApplicationException("获取到多个 Id 为" + id + "的房屋类别信息");
            }
        }

        private IdNameDTO ToDTO(IdName entity)
        {
            IdNameDTO dto = new IdNameDTO();
            dto.CreateDateTime = entity.CreateDateTime;
            dto.Id = entity.Id;
            dto.Name = entity.Name;
            dto.TypeName = entity.TypeName;
            return dto;
        }

    }
}
